package com.tencent.qf.common.util;

import com.google.common.util.concurrent.RateLimiter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author: p_haojiawu@tencent.com
 * @date: 2023-03-20 18:01
 **/

@Component
@Slf4j
public class RateLimiterUtil {

    private final RateLimiter rateLimiter;

    private final RateLimiter qwRobotRateLimiter;

    @Autowired
    public RateLimiterUtil(@Value("${api.qps.limit.qw-robot-limit}") double  qwRobotLimit,
                           @Value("${api.qps.limit.user-coupon-status-count}") Integer  userCouponStatusCountLimit){
        this.qwRobotRateLimiter = RateLimiter.create(qwRobotLimit);
        this.rateLimiter = RateLimiter.create(userCouponStatusCountLimit);
    }

    public double acquire(Integer permits){
        return rateLimiter.acquire(permits);
    }

    public double qwRobotAcquire(Integer permits){
        return qwRobotRateLimiter.acquire(permits);
    }


}
